package com.drama.api.company.dramas.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.NumberUtil;
import com.drama.api.base.Constants;
import com.drama.api.base.IDsEntity;
import com.drama.api.base.Type;
import com.drama.api.company.company.dao.CompanyDao;
import com.drama.api.company.company.domain.po.CompanyPO;
import com.drama.api.company.dramas.dao.CompanyDramasDao;
import com.drama.api.company.dramas.dao.CompanyDramasScheduleDao;
import com.drama.api.company.dramas.dao.DramasExamineDao;
import com.drama.api.company.dramas.dao.VideoLogDao;
import com.drama.api.company.dramas.domain.po.CompanyDramasPO;
import com.drama.api.company.dramas.domain.po.CompanyDramasSchedulePO;
import com.drama.api.company.dramas.domain.po.DramasExaminePO;
import com.drama.api.company.dramas.domain.po.VideoLogPO;
import com.drama.api.company.dramas.domain.vo.*;
import com.drama.api.company.dramas.service.ICompanyDramasService;
import com.drama.api.company.video.dao.CompanyVideoDao;
import com.drama.api.company.video.domain.po.CompanyVideoPO;
import com.drama.api.sys.dramas.dao.SysVideoDao;
import com.drama.api.sys.dramas.domain.po.SysVideoPO;
import com.drama.api.sys.message.service.ISysMessageService;
import com.drama.api.sys.video.dao.ResourceFileDao;
import com.drama.api.sys.video.domain.po.ResourceFilePO;
import com.drama.api.sys.video.service.IVideoTaskService;
import com.drama.api.task.DeleteFileThread;
import com.drama.exception.BusinessException;
import com.drama.util.RestTemplateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;


@Service
@Transactional
@Slf4j
public class CompanyDramasServiceImpl implements ICompanyDramasService {
    @Autowired
    private CompanyDao companyDao;
    @Autowired
    private CompanyDramasDao companyDramasDao;

    @Autowired
    private CompanyDramasScheduleDao companyDramasScheduleDao;

    @Autowired
    private CompanyVideoDao companyVideoDao;

    @Autowired
    private DramasExamineDao dramasExamineDao;
    @Autowired
    private ResourceFileDao resourceFileDao;

    @Autowired
    private ISysMessageService messageService;

    @Autowired
    private IVideoTaskService service;

    @Value("${resource.api.url}")
    private String resource_server_url;


    @Override
    public List<CompanyDramasVO> getList(CompanyDramasSearchVO searchVO) {
        if(StringUtils.isBlank(searchVO.getCompanyId())){
            searchVO.setCompanyId((String) StpUtil.getLoginId());
        }
        return companyDramasDao.getList(searchVO);
    }

    @Override
    public void add(CompanyDramasAddVO vo) {
        checkCompany();
        String maxRef=companyDramasDao.getMaxRef();
        if(StringUtils.isBlank(maxRef)){
            maxRef="D0001";
        }
        Example example=new Example(CompanyDramasPO.class);
        example.and().andEqualTo("dramaName",vo.getDramaName());
        if(companyDramasDao.selectCountByExample(example)>0){
            throw new BusinessException(Type.FAIL.getCode(),  "剧目名称不能重复！");
        }

        maxRef="D"+ NumberUtil.decimalFormat("0000", new BigDecimal(maxRef.replaceAll("D","0")).add(BigDecimal.ONE).intValue());
        CompanyDramasPO companyDramasPO=new CompanyDramasPO();
        BeanUtils.copyProperties(vo, companyDramasPO);
        companyDramasPO.preInsert();
        companyDramasPO.setCompanyId((String) StpUtil.getLoginId());
        companyDramasPO.setDramaState(1);
        companyDramasPO.setDramaRef(maxRef);
        companyDramasPO.setDramasSchedule(2);
        companyDramasPO.setScheduleDateA(companyDramasPO.getUpdateDate());
        companyDramasPO.setScheduleDate(companyDramasPO.getUpdateDate());
        int i = companyDramasDao.insertSelective(companyDramasPO);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
        DramasExaminePO dramasExaminePO=new DramasExaminePO();
        dramasExaminePO.setDramaId(companyDramasPO.getId());
        dramasExamineDao.insertSelective(dramasExaminePO);
        //更新视频信息
        updateDramasVideo(vo.getVideoList(),companyDramasPO.getId());
        //添加提审流程
        addDramasSchedule(companyDramasPO,companyDramasPO.getUpdateDate(),"剧目提交",1);
        CompanyPO companyPO=companyDao.selectByPrimaryKey(StpUtil.getLoginId());
        messageService.addSysMsg("剧目提审通知",companyPO.getCompanyName()+"已提审剧目（剧名："+companyDramasPO.getDramaName()+"），请尽快前往【剧目管理】中进行处理。","/sys/drama",1);
        //新增内存
        if(StringUtils.isNotBlank(vo.getDramaEmpowerUrl())){
            Example fileExample=new Example(ResourceFilePO.class);
            fileExample.and().andEqualTo("fileFullPath",vo.getDramaEmpowerUrl());
            ResourceFilePO companyFilePO=resourceFileDao.selectByExample(fileExample).get(0);
            companyPO.setStorageNum(new BigDecimal(companyPO.getStorageNum()).add(new BigDecimal(companyFilePO.getFileSize())).toString());
        }
        if(StringUtils.isNotBlank(vo.getDramaFilingsUrl())){
            Example fileExample=new Example(ResourceFilePO.class);
            fileExample.and().andEqualTo("fileFullPath",vo.getDramaFilingsUrl());
            ResourceFilePO companyFilePO=resourceFileDao.selectByExample(fileExample).get(0);
            companyPO.setStorageNum(new BigDecimal(companyPO.getStorageNum()).add(new BigDecimal(companyFilePO.getFileSize())).toString());
        }

        if(StringUtils.isNotBlank(vo.getDramaImg())){
            Example fileExample=new Example(ResourceFilePO.class);
            fileExample.and().andEqualTo("fileFullPath",vo.getDramaImg());
            ResourceFilePO companyFilePO=resourceFileDao.selectByExample(fileExample).get(0);
            companyPO.setStorageNum(new BigDecimal(companyPO.getStorageNum()).add(new BigDecimal(companyFilePO.getFileSize())).toString());
        }
        companyPO.preUpdate();
        companyDao.updateByPrimaryKeySelective(companyPO);

    }

    @Override
    public void update(CompanyDramasUpdateVO vo) {

        checkCompany();
        CompanyDramasPO companyDramasPO=companyDramasDao.selectByPrimaryKey(vo.getId());
        if(companyDramasPO.getDramaState()!=2&&companyDramasPO.getDramaState()!=9){
            throw new BusinessException(Type.FAIL.getCode(),  "当前剧目状态不可以重新提交！");
        }
        Example example=new Example(CompanyDramasPO.class);
        example.and().andEqualTo("dramaName",vo.getDramaName());
        List<CompanyDramasPO> oldCompanyVideoPOList=companyDramasDao.selectByExample(example);
        if(oldCompanyVideoPOList.size()>1){
            throw new BusinessException(Type.FAIL.getCode(),  "剧目名称不能重复！");
        }
        if(oldCompanyVideoPOList.size()==1){
            if(oldCompanyVideoPOList.get(0)!=null&&!oldCompanyVideoPOList.get(0).getId().equals(vo.getId())){
                throw new BusinessException(Type.FAIL.getCode(),  "剧目名称不能重复！");
            }
        }
        BeanUtils.copyProperties(vo, companyDramasPO);
        companyDramasPO.preUpdate();
        companyDramasPO.setDramaState(1);
        companyDramasPO.setDramasSchedule(2);
        companyDramasPO.setScheduleDateA(companyDramasPO.getUpdateDate());
        int i = companyDramasDao.updateByPrimaryKeySelective(companyDramasPO);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
        //更新视频信息
        deleteDramasVideo(vo.getId());
        updateDramasVideo(vo.getVideoList(),companyDramasPO.getId());
        //添加提审流程
        addDramasSchedule(companyDramasPO,companyDramasPO.getUpdateDate(),"剧目提交",1);
        CompanyPO companyPO=companyDao.selectByPrimaryKey(StpUtil.getLoginId());
        messageService.addSysMsg("剧目提审通知",companyPO.getCompanyName()+"已提审剧目（剧名："+companyDramasPO.getDramaName()+"），请尽快前往【剧目管理】中进行处理。","/sys/drama",1);
    }

    private void checkCompany() {
        CompanyPO companyPO = companyDao.selectByPrimaryKey(StpUtil.getLoginId());
        if (companyPO == null) {
            throw new BusinessException(Type.PERMISSIONS_VALIDATE_FAIL);
        }
        if (companyPO.getIsArraign() == null || companyPO.getIsArraign() != 1) {
            throw new BusinessException(Type.FAIL.getCode(), "当前账号没有提审权限！");
        }
    }

    @Override
    public void delete(String id) {
        CompanyDramasPO companyDramasPO=companyDramasDao.selectByPrimaryKey(id);
        if(companyDramasPO==null){
            throw new BusinessException(Type.NOT_FOUND_ERROR);
        }
        if(companyDramasPO.getDramaState()!=2&&companyDramasPO.getDramaState()!=8&&companyDramasPO.getDramaState()!=9){
            throw new BusinessException(Type.FAIL.getCode(),  "当前剧目状态不可以删除！");
        }
        companyDramasPO.preDelete();
//        int i = companyDramasDao.deleteByPrimaryKey(id);
        int i = companyDramasDao.updateByPrimaryKeySelective(companyDramasPO);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
//        //释放空间
//        CompanyPO companyPO=companyDao.selectByPrimaryKey(companyDramasPO.getCompanyId());
//
//        Example example=new Example(CompanyVideoPO.class);
//        example.and().andEqualTo("dramaId",companyDramasPO.getId());
//        List<CompanyVideoPO> videoList=companyVideoDao.selectByExample(example);
//        if(videoList!=null&&videoList.size()>0){
//            for (CompanyVideoPO videoPO:videoList){
//                ResourceFilePO resourceFilePO=resourceFileDao.selectByPrimaryKey(videoPO.getVideoFileId());
//                companyPO.setStorageNum(new BigDecimal(companyPO.getStorageNum()).subtract(new BigDecimal(resourceFilePO.getFileSize())).toString());
//                Constants.executor.execute(new DeleteFileThread(new RestTemplateUtils(),resource_server_url,resourceFilePO.getFileFullPath()));
//                resourceFileDao.deleteByPrimaryKey(resourceFilePO.getId());
//                //如果有转码视频
//                if(StringUtils.isNotBlank(videoPO.getVideoSize())&&StringUtils.isNotBlank(videoPO.getVideoUrl())){
//                    companyPO.setStorageNum(new BigDecimal(companyPO.getStorageNum()).subtract(new BigDecimal(videoPO.getVideoSize())).toString());
//                    Constants.executor.execute(new DeleteFileThread(new RestTemplateUtils(),resource_server_url,videoPO.getVideoUrl()));
//                }
//                companyVideoDao.deleteByPrimaryKey(videoPO.getId());
//            }
//        }
//
//
//        if(StringUtils.isNotBlank(companyDramasPO.getDramaUrl())&&StringUtils.isNotBlank(companyDramasPO.getRemarks())){
//            companyPO.setStorageNum(new BigDecimal(companyPO.getStorageNum()).subtract(new BigDecimal(companyDramasPO.getRemarks())).toString());
//            ResourceFilePO companyFilePO=resourceFileDao.selectByPrimaryKey(companyDramasPO.getDramaFileId());
//            if(!companyFilePO.getFileFullPath().equals(companyDramasPO.getDramaUrl())){
//                Constants.executor.execute(new DeleteFileThread(new RestTemplateUtils(),resource_server_url,companyDramasPO.getDramaUrl()));
//            }
//            resourceFileDao.deleteByPrimaryKey(companyDramasPO.getDramaFileId());
//        }
//
//        //释放剧目资源内存
//        if(StringUtils.isNotBlank(companyDramasPO.getDramaEmpowerUrl())){
//            Example fileExample=new Example(ResourceFilePO.class);
//            fileExample.and().andEqualTo("fileFullPath",companyDramasPO.getDramaEmpowerUrl());
//            ResourceFilePO companyFilePO=resourceFileDao.selectByExample(fileExample).get(0);
//            resourceFileDao.deleteByPrimaryKey(companyFilePO.getId());
//            companyPO.setStorageNum(new BigDecimal(companyPO.getStorageNum()).subtract(new BigDecimal(companyFilePO.getFileSize())).toString());
//            Constants.executor.execute(new DeleteFileThread(new RestTemplateUtils(),resource_server_url,companyDramasPO.getDramaEmpowerUrl()));
//        }
//        if(StringUtils.isNotBlank(companyDramasPO.getDramaFilingsUrl())){
//            Example fileExample=new Example(ResourceFilePO.class);
//            fileExample.and().andEqualTo("fileFullPath",companyDramasPO.getDramaFilingsUrl());
//            ResourceFilePO companyFilePO=resourceFileDao.selectByExample(fileExample).get(0);
//            resourceFileDao.deleteByPrimaryKey(companyFilePO.getId());
//            companyPO.setStorageNum(new BigDecimal(companyPO.getStorageNum()).subtract(new BigDecimal(companyFilePO.getFileSize())).toString());
//            Constants.executor.execute(new DeleteFileThread(new RestTemplateUtils(),resource_server_url,companyDramasPO.getDramaFilingsUrl()));
//        }
//
//        if(StringUtils.isNotBlank(companyDramasPO.getDramaImg())){
//            Example fileExample=new Example(ResourceFilePO.class);
//            fileExample.and().andEqualTo("fileFullPath",companyDramasPO.getDramaImg());
//            ResourceFilePO companyFilePO=resourceFileDao.selectByExample(fileExample).get(0);
//            resourceFileDao.deleteByPrimaryKey(companyFilePO.getId());
//            companyPO.setStorageNum(new BigDecimal(companyPO.getStorageNum()).subtract(new BigDecimal(companyFilePO.getFileSize())).toString());
//            Constants.executor.execute(new DeleteFileThread(new RestTemplateUtils(),resource_server_url,companyDramasPO.getDramaImg()));
//        }
//        companyPO.preUpdate();
//        companyDao.updateByPrimaryKeySelective(companyPO);
//        deleteDramasVideo(id);
    }



    @Override
    public void go_live(CompanyDramasLiveVO vo) {
        CompanyDramasPO companyDramasPO=companyDramasDao.selectByPrimaryKey(vo.getId());
        if(companyDramasPO==null){
            throw new BusinessException(Type.NOT_FOUND_ERROR);
        }
        if(companyDramasPO.getDramaState()!=4&&companyDramasPO.getDramaState()!=6){
            throw new BusinessException(Type.FAIL.getCode(),  "当前剧目状态不可以申请上线！");
        }
        CompanyPO companyPO=companyDao.selectByPrimaryKey(companyDramasPO.getCompanyId());
        BeanUtils.copyProperties(vo, companyDramasPO);
        companyDramasPO.preUpdate();
        companyDramasPO.setDramaState(5);
        companyDramasPO.setDramasSchedule(5);
        companyDramasPO.setScheduleDateD(companyDramasPO.getUpdateDate());
        companyDramasPO.setScheduleDate(companyDramasPO.getUpdateDate());
        if(StringUtils.isNotBlank(vo.getDramaUrl())){
            ResourceFilePO companyFilePO=resourceFileDao.selectByPrimaryKey(vo.getDramaFileId());
            companyDramasPO.setRemarks(companyFilePO.getFileSize());
            companyPO.setStorageNum(new BigDecimal(companyPO.getStorageNum()).add(new BigDecimal(companyFilePO.getFileSize())).toString());
        }
        int i = companyDramasDao.updateByPrimaryKeySelective(companyDramasPO);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
        Example example=new Example(CompanyVideoPO.class);
        example.and().andEqualTo("dramaId",companyDramasPO.getId()).andGreaterThan("videoGather",vo.getDramaFree());
        CompanyVideoPO updateVideo=new CompanyVideoPO();
        updateVideo.setVideoPrice(vo.getDramaPrice());
        companyVideoDao.updateByExampleSelective(updateVideo,example);
        addDramasSchedule(companyDramasPO,companyDramasPO.getUpdateDate(),"发起上线申请",4);
        service.addDrama(companyDramasPO.getId());//添加剧目片花转码

        messageService.addSysMsg("剧目上线申请通知",companyPO.getCompanyName()+"发起剧目（剧名："+companyDramasPO.getDramaName()+"）的上线申请，请尽快前往【剧目管理】中进行处理。","/sys/drama",2);
        companyPO.preUpdate();
        companyDao.updateByPrimaryKeySelective(companyPO);
    }


    @Override
    public void go_live_price(CompanyDramasLiveVO vo) {
        CompanyDramasPO companyDramasPO=companyDramasDao.selectByPrimaryKey(vo.getId());
        Example example=new Example(CompanyVideoPO.class);
        example.and().andEqualTo("dramaId",companyDramasPO.getId()).andGreaterThan("videoGather",vo.getDramaFree());
        CompanyVideoPO updateVideo=new CompanyVideoPO();
        updateVideo.setVideoPrice(vo.getDramaPrice());
        companyVideoDao.updateByExampleSelective(updateVideo,example);
    }
    @Autowired
    private VideoLogDao videoLogDao;

    @Autowired
    SysVideoDao sysVideoDao;

    @Override
    public void revoke(String id) {
        CompanyDramasPO companyDramasPO=companyDramasDao.selectByPrimaryKey(id);
        if(companyDramasPO==null){
            throw new BusinessException(Type.NOT_FOUND_ERROR);
        }
        if(companyDramasPO.getDramaState()!=1){
            throw new BusinessException(Type.FAIL.getCode(),  "当前剧目状态不可以撤回！");
        }
        companyDramasPO.setDramaState(9);
        companyDramasPO.preUpdate();
        int i = companyDramasDao.updateByPrimaryKeySelective(companyDramasPO);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }

        //整理日志
        Example videLogExample=new Example(VideoLogPO.class);
        videLogExample.and().andEqualTo("dramaId",companyDramasPO.getId());
        videoLogDao.deleteByExample(videLogExample);
        videoLogDao.insertByDramaId(companyDramasPO.getId());

        Example example=new Example(SysVideoPO.class);
        example.and().andEqualTo("dramaId",companyDramasPO.getId());
        SysVideoPO sysVideoPO=new SysVideoPO();
        sysVideoPO.setVideoState(2);
        sysVideoPO.setDramaId("");
        sysVideoDao.updateByExampleSelective(sysVideoPO,example);

        addDramasSchedule(companyDramasPO,companyDramasPO.getUpdateDate(),"剧目撤回",2);
    }

    @Override
    public void deleteBatch(IDsEntity iDsEntity) {
        if(iDsEntity.getIds()!=null&&iDsEntity.getIds().size()>0){
            for (String id:iDsEntity.getIds()){
                this.delete(id);
            }

        }
    }

    @Override
    public CompanyDramasPO getDramas(String dramaId) {
        return companyDramasDao.selectByPrimaryKey(dramaId);
    }

    @Override
    public void recover(String id) {
        CompanyDramasPO companyDramasPO=companyDramasDao.selectByPrimaryKey(id);
        if(companyDramasPO==null){
            throw new BusinessException(Type.NOT_FOUND_ERROR);
        }
        companyDramasPO.setDelFlag(Boolean.FALSE);
        companyDramasPO.preUpdate();
        int i = companyDramasDao.updateByPrimaryKeySelective(companyDramasPO);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
    }

    @Override
    public void deletes(String id) {
        CompanyDramasPO companyDramasPO=companyDramasDao.selectByPrimaryKey(id);
        if(companyDramasPO==null){
            throw new BusinessException(Type.NOT_FOUND_ERROR);
        }
        if(!companyDramasPO.isDelFlag()){
            throw new BusinessException(Type.FAIL.getCode(),  "当前剧目状态不可以删除！");
        }
        companyDramasPO.preDelete();
        int i = companyDramasDao.deleteByPrimaryKey(id);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
        //释放空间
        CompanyPO companyPO=companyDao.selectByPrimaryKey(companyDramasPO.getCompanyId());

        Example example=new Example(CompanyVideoPO.class);
        example.and().andEqualTo("dramaId",companyDramasPO.getId());
        List<CompanyVideoPO> videoList=companyVideoDao.selectByExample(example);
        if(videoList!=null&&videoList.size()>0){
            for (CompanyVideoPO videoPO:videoList){
                ResourceFilePO resourceFilePO=resourceFileDao.selectByPrimaryKey(videoPO.getVideoFileId());
                companyPO.setStorageNum(new BigDecimal(companyPO.getStorageNum()).subtract(new BigDecimal(resourceFilePO.getFileSize())).toString());
                Constants.executor.execute(new DeleteFileThread(new RestTemplateUtils(),resource_server_url,resourceFilePO.getFileFullPath()));
                resourceFileDao.deleteByPrimaryKey(resourceFilePO.getId());
                //如果有转码视频
                if(StringUtils.isNotBlank(videoPO.getVideoSize())&&StringUtils.isNotBlank(videoPO.getVideoUrl())){
                    companyPO.setStorageNum(new BigDecimal(companyPO.getStorageNum()).subtract(new BigDecimal(videoPO.getVideoSize())).toString());
                    Constants.executor.execute(new DeleteFileThread(new RestTemplateUtils(),resource_server_url,videoPO.getVideoUrl()));
                }
                companyVideoDao.deleteByPrimaryKey(videoPO.getId());
            }
        }


        if(StringUtils.isNotBlank(companyDramasPO.getDramaUrl())&&StringUtils.isNotBlank(companyDramasPO.getRemarks())){
            companyPO.setStorageNum(new BigDecimal(companyPO.getStorageNum()).subtract(new BigDecimal(companyDramasPO.getRemarks())).toString());
            ResourceFilePO companyFilePO=resourceFileDao.selectByPrimaryKey(companyDramasPO.getDramaFileId());
            if(!companyFilePO.getFileFullPath().equals(companyDramasPO.getDramaUrl())){
                Constants.executor.execute(new DeleteFileThread(new RestTemplateUtils(),resource_server_url,companyDramasPO.getDramaUrl()));
            }
            resourceFileDao.deleteByPrimaryKey(companyDramasPO.getDramaFileId());
        }

        //释放剧目资源内存
        if(StringUtils.isNotBlank(companyDramasPO.getDramaEmpowerUrl())){
            Example fileExample=new Example(ResourceFilePO.class);
            fileExample.and().andEqualTo("fileFullPath",companyDramasPO.getDramaEmpowerUrl());
            ResourceFilePO companyFilePO=resourceFileDao.selectByExample(fileExample).get(0);
            resourceFileDao.deleteByPrimaryKey(companyFilePO.getId());
            companyPO.setStorageNum(new BigDecimal(companyPO.getStorageNum()).subtract(new BigDecimal(companyFilePO.getFileSize())).toString());
            Constants.executor.execute(new DeleteFileThread(new RestTemplateUtils(),resource_server_url,companyDramasPO.getDramaEmpowerUrl()));
        }
        if(StringUtils.isNotBlank(companyDramasPO.getDramaFilingsUrl())){
            Example fileExample=new Example(ResourceFilePO.class);
            fileExample.and().andEqualTo("fileFullPath",companyDramasPO.getDramaFilingsUrl());
            ResourceFilePO companyFilePO=resourceFileDao.selectByExample(fileExample).get(0);
            resourceFileDao.deleteByPrimaryKey(companyFilePO.getId());
            companyPO.setStorageNum(new BigDecimal(companyPO.getStorageNum()).subtract(new BigDecimal(companyFilePO.getFileSize())).toString());
            Constants.executor.execute(new DeleteFileThread(new RestTemplateUtils(),resource_server_url,companyDramasPO.getDramaFilingsUrl()));
        }

        if(StringUtils.isNotBlank(companyDramasPO.getDramaImg())){
            Example fileExample=new Example(ResourceFilePO.class);
            fileExample.and().andEqualTo("fileFullPath",companyDramasPO.getDramaImg());
            ResourceFilePO companyFilePO=resourceFileDao.selectByExample(fileExample).get(0);
            resourceFileDao.deleteByPrimaryKey(companyFilePO.getId());
            companyPO.setStorageNum(new BigDecimal(companyPO.getStorageNum()).subtract(new BigDecimal(companyFilePO.getFileSize())).toString());
            Constants.executor.execute(new DeleteFileThread(new RestTemplateUtils(),resource_server_url,companyDramasPO.getDramaImg()));
        }
        companyPO.preUpdate();
        companyDao.updateByPrimaryKeySelective(companyPO);
        deleteDramasVideo(id);
    }

    @Override
    public List<CompanyDramasCountVO> count(CompanyDramasCountSearchVO searchVO) {
        List<CompanyDramasCountVO> list=companyDramasDao.getCount(searchVO);
        HashMap<Integer,Integer> maps=new HashMap<>();
        if(list!=null&&list.size()>0){
            maps=(HashMap<Integer, Integer>) list.stream().collect(Collectors.toMap(CompanyDramasCountVO::getDramaState,CompanyDramasCountVO::getNum));
        }
        List<CompanyDramasCountVO> resultList=new ArrayList<>();
        for (int i = 1; i <10 ; i++) {
            CompanyDramasCountVO dramasCountVO=new CompanyDramasCountVO();
            dramasCountVO.setDramaState(i);
            dramasCountVO.setNum(0);
            if(maps.containsKey(i)){
                dramasCountVO.setNum(maps.get(i));
            }
            resultList.add(dramasCountVO);
        }
        return resultList;
    }

    @Override
    public CompanyDramasScheduleVO failed_desc(String id) {
        DramasExaminePO dramasExaminePO=dramasExamineDao.selectByPrimaryKey(id);
        if(dramasExaminePO==null){
            throw new BusinessException(Type.NOT_FOUND_ERROR);
        }
        CompanyDramasScheduleVO companyDramasScheduleVO=new CompanyDramasScheduleVO();
        BeanUtils.copyProperties(dramasExaminePO, companyDramasScheduleVO);
        return companyDramasScheduleVO;
    }

    @Override
    public String reject_desc(String id) {

        DramasExaminePO dramasExaminePO=dramasExamineDao.selectByPrimaryKey(id);
        if(dramasExaminePO==null){
            throw new BusinessException(Type.NOT_FOUND_ERROR);
        }
        return dramasExaminePO.getRejectDesc();
    }

    private void deleteDramasVideo(String id) {
        Example example = new Example(CompanyVideoPO.class);
        example.and().andEqualTo("dramaId", id);
        CompanyVideoPO companyVideoPO = new CompanyVideoPO();
        companyVideoPO.setVideoState(2);
        companyVideoPO.setDramaId("");
        companyVideoDao.updateByExampleSelective(companyVideoPO, example);
    }
    private void updateDramasVideo(List<CompanyDramasVideoAddVO> videoList,String dramasId) {
        if (videoList != null && videoList.size() > 0) {
            for (CompanyDramasVideoAddVO videoAddVO : videoList) {
                CompanyVideoPO companyVideoPO = new CompanyVideoPO();
                companyVideoPO.setVideoState(1);
                companyVideoPO.setVideoGather(videoAddVO.getVideoGather());
                companyVideoPO.setId(videoAddVO.getId());
                companyVideoPO.setDramaId(dramasId);
                companyVideoPO.preUpdate();
                companyVideoDao.updateByPrimaryKeySelective(companyVideoPO);
            }
        }
    }

    /**
     * 添加流程日志
     * @param companyDramasPO
     */
    public void addDramasSchedule(CompanyDramasPO companyDramasPO, Date updateDate, String title, Integer type) {
        CompanyDramasSchedulePO companyDramasSchedulePO = new CompanyDramasSchedulePO();
        companyDramasSchedulePO.setDramaId(companyDramasPO.getId());
        companyDramasSchedulePO.setLogTitle(title);
        companyDramasSchedulePO.setLogType(type);
        companyDramasSchedulePO.setLogState(3);
        companyDramasSchedulePO.preInsert();
        companyDramasSchedulePO.setCreateDate(updateDate);
        companyDramasSchedulePO.setUpdateDate(updateDate);
        int j = companyDramasScheduleDao.insertSelective(companyDramasSchedulePO);
        if (j == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
    }

    @Override
    public void checkName(String name) {
        Example example=new Example(CompanyDramasPO.class);
        example.and().andEqualTo("dramaName",name);
        if(companyDramasDao.selectCountByExample(example)>0){
            throw new BusinessException(Type.FAIL.getCode(),  "剧目名称重复！");
        }

    }




}